from utils.GetKeyword import GetKeyword
from utils.SendMethod import SendMethod


class MemberInterface:
    def __init__(self, url):
        self.url = url

    # 获取验证码接口返回数据
    def get_autocode(self, telephone):
        method = "get"
        url = self.url + "/sso/getAuthCode"
        params = {'telephone': telephone}
        return SendMethod.send_method(method, url, params=params)

    # 提取验证码
    def get_verify_code(self, telephone):
        resp = self.get_autocode(telephone)
        return GetKeyword.getkeyword(resp, 'data')

    # 新用户注册
    def register(self, payload):
        method = 'post'
        url = self.url + "/sso/register"
        return SendMethod.send_method(method, url, data=payload)

    # 用户登录
    def login(self, username, password):
        method = "post"
        url = self.url + "/sso/login"
        payload = {
            'username': username,
            'password': password
        }
        return SendMethod.send_method(method, url, data=payload)

    # 提取token
    def get_token(self, username, password):
        resp = self.login(username, password)
        token = GetKeyword.getkeyword(resp, 'token')
        return {'Authorization': f'Bearer {token}'}


if __name__ == '__main__':
    url = 'http://47.108.206.100:8085'
    telephone = '18009187516'
    mi = MemberInterface(url)
    regi_info = {
        'username': 'KFC7516',
        'password': '654321',
        'telephone': telephone,
        'authCode': mi.get_verify_code(telephone)
    }
    # print(mi.get_autocode(telephone))
    # print(mi.get_verify_code(telephone))
    # print(mi.register(regi_info))
    print(mi.login(username=regi_info['username'], password=regi_info['password']))
    print(mi.get_token(username=regi_info['username'], password=regi_info['password']))
